package util;

import com.google.gson.Gson;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class HttpsUtil {

    public static void main(String[] args) {
        for (; ; ) {
            YuQueDTO yuQueDTO = httpYuQue();
            if (yuQueDTO.getData() != null && StringUtils.isNotBlank(yuQueDTO.getData().password)) {
                System.out.println("-------------------:密码：" + yuQueDTO.getData().password);
                if (yuQueDTO.getData().password.toLowerCase().startsWith("st")) {
                    break;
                }
            }
        }
    }

    public static YuQueDTO httpYuQue() {
        String url = "https://www.yuque.com/api/share/c367fefa-e2c4-45e9-b1f3-5198fe8c8d6b/password";

        //设置Http Header
        HttpHeaders headers = new HttpHeaders();
        Map<String, Object> map = new HashMap<>();
        map.put("enable", true);
        //设置请求媒体数据类型
        headers.setContentType(MediaType.APPLICATION_JSON);
        //设置返回媒体数据类型
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("referer", "https://www.yuque.com/wangqingfeng-43psx/gxo1rm");
        headers.add("cookie", "UM_distinctid=17ca2cf4307347-0156308140623a-d7e1739-1fa400-17ca2cf4308b46; lang=zh-cn; _yuque_session=swEVJ-9uirsR3D9zitM2Pms4ALeEgLRnjzn1AAjm7seK77Kr2NZnNMIJFfiyzKpakdBLB6jbdWM200Pz_V2v7Q==; acw_tc=0a5510bc16404949523978303e7687d0dc450502bfc89e6e97f45885b74bc1; yuque_ctoken=DnXg1erZznPVZLEz9O9UqKkr; CNZZDATA1272061571=1009910791-1634819015-%7C1640486108; _TRACERT_COOKIE__SESSION=4c982340-c268-4e52-83d7-cf2a2a7b48b4; tree=a385%01d7014269-88f4-4f67-b82e-df951b651699%016");

        HttpEntity<String> formEntity = new HttpEntity<String>(new Gson().toJson(map), headers);

        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(url, formEntity, YuQueDTO.class);
    }

    @Data
    public static class YuQueDTO {
        private DataDto data;

    }

    @Data
    public static class DataDto {
        private String password;
    }
}
